<?php

namespace App\Http\Controllers;

use App\Http\Requests\UserUpdate;
use App\Models\User;
use App\utils\ImageUploader;
use Illuminate\Http\Request;

class UsersController extends Controller
{

    public function __contruct()
    {
        $this->middleware(['auth'])->except('show');

    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request,User $user)
    {
        //



        return view('users.show',compact('user'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(User $user)
    {
        $this->authorize('update',$user);
        return view('users.edit',compact('user'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UserUpdate $request, User $user, ImageUploader $uploader)
    {
        $this->authorize('update',$user);

        $request->validated();
        $data = $request->all();


        if(isset($data['avatar'])){
            $avatar = $uploader->save($data['avatar'],'avatars',$user->id,600,true);
            $data['avatar'] = $avatar['path'];
        }

        $user->update($data);

        return redirect()->route('users.show',$user->id)
            ->with('success','修改成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
